/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: menu.c
> Author:z1f
> Mail: 
> Created Time: 2024年12月04日 星期三 11时57分04秒
***************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <signal.h>
#include "cJSON.h"

// 假设的用户结构体
typedef struct User{
	char type[20];
    char username[20];
    char passwd[20];
} USER;

// 函数声明
int menu();
int usr_menu();
int user_menu();
int adm_menu();
int admin_menu();
char *packJson(const char *key, const char *value);
int parseJson(const char *p);
int send_msg(const char *jsonMsg);
int user_reg();
int user_login();
int admin_reg();
int admin_login();

int menu() {
    system("clear");
    int op = 0;
    printf("1------------用户\n");
    printf("2------------管理员\n");
    printf("3------------退出系统\n");

    while (scanf("%d", &op)) {
        if (op <= 0 || op > 3) {
            printf("请输入1～3.\n");
        } else {
            break;
        }
    }
    switch (op) {
        case 1: // 用户菜单
            while (usr_menu());
            break;
        case 2: // 管理员菜单
            while (adm_menu());
            break;
        case 3: // 退出系统
            exit(1);
    }
    return 1;
}

int usr_menu() {
    sleep(1);
    system("clear");
    int op1 = 0;
    printf("1-------------注册\n");
    printf("2-------------登陆\n");
    printf("3-------------返回\n");
    scanf("%d", &op1);
    getchar();
    if (op1 == 0)
        return 1;
    switch (op1) {
        case 1: // 注册用户
            user_reg();
            break;
        case 2: // 用户登录
            if (user_login() == 0) {
                while (user_menu());
            }
            break;
        case 3: // 返回上一级菜单
            return 0;
    }
    return 1;
}

int user_menu() {
    sleep(1);
    system("clear");
    int op2 = 0;
    printf("1-------------电池板状态\n");
    printf("2-------------蓄电池当前存储电量\n");
    printf("3-------------环境参数监控\n");
    printf("4-------------历史记录\n");
    printf("5-------------数据查询\n");
    printf("6-------------返回\n");
    scanf("%d", &op2);
    getchar();

    char *key = NULL;
    char *value = "请求数据";

    switch (op2) {
        case 1:
            key = "battery_status";
            break;
        case 2:
            key = "battery_storage";
            break;
        case 3:
            key = "environment_monitor";
            break;
        case 4:
            key = "history_records";
            break;
        case 5:
            key = "data_query";
            break;
        case 6:
            return 0; // 返回上一级菜单
        default:
            printf("无效选项，请重新输入。\n");
            break;
    }

    if (key != NULL) {
        send_msg(packJson(key, value));
    }

    return 1;
}

int adm_menu() {
    system("clear");
    int op3 = 0;
    printf("1------------管理员注册.\n");
    printf("2------------管理员登陆.\n");
    printf("3------------返回.\n");
    scanf("%d", &op3);
    getchar();

    switch (op3) {
        case 1:
            admin_reg();
            break;
        case 2:
            if (admin_login() == 0) 
			{
                while (admin_menu());
            }
            break;
        case 3:
            return 0; // 返回上一级菜单
        default:
            printf("无效选项，请重新输入。\n");
            break;
    }
    return 1;
}

int admin_menu() {
    system("clear");
    int op4 = 0;

    printf("1------------故障报警\n");
    printf("2------------查看系统日志\n");
    printf("3------------用户信息修改\n");
    printf("4------------用户信息查询\n");
    printf("5------------返回\n");

    scanf("%d", &op4);
    getchar();

    char *key = NULL;
    char *value = "请求数据";

    switch (op4) {
        case 1:
            key = "fault_alarm";
            break;
        case 2:
            key = "system_log";
            break;
        case 3:
            key = "user_info_modify";
            break;
        case 4:
            key = "user_info_query";
            break;
        case 5:
            return 0; // 返回上一级菜单
        default:
            printf("无效选项，请重新输入。\n");
            break;
    }

    if (key != NULL) {
        send_msg(packJson(key, value));
    }

    return 1;
}

char *packJson(const char *key, const char *value) {
    char *p = (char *)malloc(1024);
    cJSON *root = cJSON_CreateObject();

    cJSON *keyItem = cJSON_CreateString(key);
    cJSON *valueItem = cJSON_CreateString(value);

    cJSON_AddItemToObject(root, key, keyItem);
    cJSON_AddItemToObject(root, "value", valueItem);

    strcpy(p, cJSON_Print(root));

    cJSON_Delete(root);
    return p;
}

int parseJson(const char *p) {
    cJSON *root = cJSON_Parse(p);

    cJSON *code = cJSON_GetObjectItem(root, "returncode");

    int cd = cJSON_GetNumberValue(code);
    return cd;
}

int send_msg(const char *jsonMsg) {
    printf("发送的消息: %s\n", jsonMsg);

    int sc = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in caddr;
    caddr.sin_family = AF_INET;
    caddr.sin_port = htons(8888);
    caddr.sin_addr.s_addr = inet_addr("192.168.0.193");
    int isconnect = connect(sc, (struct sockaddr *)&caddr, sizeof(caddr));
    if (isconnect == -1) {
        perror("connect");
        free((void *)jsonMsg);
        return -1;
    }
    printf("连接成功\n");
    char rev_buf[1024] = {0};
    int ws = write(sc, jsonMsg, strlen(jsonMsg));
    printf("write size:%d\n", ws);

    int rs = read(sc, rev_buf, sizeof(rev_buf));
    printf("read size:%d\n", rs);

    // 解包
    int ret = parseJson(rev_buf);
    printf("服务器响应: %s\n", rev_buf);

    close(sc);
    free((void *)jsonMsg);
    return ret;
}

int user_reg() {
    USER s;
    printf("请输入用户名: ");
    scanf("%s", s.username);
    getchar();
    printf("请输入密码: ");
    scanf("%s", s.passwd);
    getchar();

    char *jsonMsg = packJson("register", s.username);
    int ret = send_msg(jsonMsg);

    if (ret == 0) {
        printf("注册成功\n");
    } else {
        printf("注册失败\n");
    }

    return ret;
}

int user_login() {
    USER s;
    printf("请输入用户名: ");
    scanf("%s", s.username);
    getchar();
    printf("请输入密码: ");
    scanf("%s", s.passwd);
    getchar();

    char *jsonMsg = packJson("login", s.username);
    int ret = send_msg(jsonMsg);

    if (ret == 0) {
        printf("登录成功\n");
    } else {
        printf("登录失败\n");
    }

    return ret;
}

int admin_reg() {
    USER s;
    printf("请输入管理员用户名: ");
    scanf("%s", s.username);
    getchar();
    printf("请输入管理员密码: ");
    scanf("%s", s.passwd);
    getchar();

    char *jsonMsg = packJson("admin_register", s.username);
    int ret = send_msg(jsonMsg);

    if (ret == 0) {
        printf("管理员注册成功\n");
    } else {
        printf("管理员注册失败\n");
    }

    return ret;
}

int admin_login() {
    USER s;
    printf("请输入管理员用户名: ");
    scanf("%s", s.username);
    getchar();
    printf("请输入管理员密码: ");
    scanf("%s", s.passwd);
    getchar();

    char *jsonMsg = packJson("admin_login", s.username);
    int ret = send_msg(jsonMsg);

        if (ret == 0) {
        printf("管理员登录成功\n");
    } else {
        printf("管理员登录失败\n");
    }

    return ret;
}

int main() {
    while (menu());
    return 0;
}